//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package idea_dao

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_idea_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type AppraiseDbItem struct {
	IdeaAppraiseId string `bson:"_id"`
	IdeaId         string `bson:"ideaId"`
	MemberUserId   string `bson:"memberUserId"`
	AppraiseType   uint32 `bson:"appraiseType"`
	TimeStamp      int64  `bson:"timeStamp"`
}

func (item *AppraiseDbItem) ToAppraise() *project_idea_api.Appraise {
	return &project_idea_api.Appraise{
		MemberUserId: item.MemberUserId,
		AppraiseType: project_idea_api.APPRAISE_TYPE(item.AppraiseType),
		TimeStamp:    item.TimeStamp,
	}
}

type _AppraiseDao struct {
	mc *mongo.Collection
}

func newAppraiseDao(db *mongo.Database) (*_AppraiseDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_IDEA + "appraise")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"ideaId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_AppraiseDao{mc: mc}, nil
}

func (db *_AppraiseDao) Set(ctx context.Context, dbItem *AppraiseDbItem) error {
	dbItem.IdeaAppraiseId = db.genIdeaAppraiseId(dbItem.IdeaId, dbItem.MemberUserId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.IdeaAppraiseId, "ideaId": dbItem.IdeaId},
		bson.M{"$set": dbItem}, options.Update().SetUpsert(true))
	return err
}

func (db *_AppraiseDao) genIdeaAppraiseId(ideaId, memberUserId string) string {
	return fmt.Sprintf("%s:%s", ideaId, memberUserId)
}

func (db *_AppraiseDao) CountByIdea(ctx context.Context, ideaId string, appraiseType uint32) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"ideaId": ideaId, "appraiseType": appraiseType})
	return uint32(count), err
}

func (db *_AppraiseDao) Get(ctx context.Context, ideaId, memberUserId string) (*AppraiseDbItem, error) {
	ideaAppraiseId := db.genIdeaAppraiseId(ideaId, memberUserId)
	retItem := &AppraiseDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": ideaAppraiseId, "ideaId": ideaId}).Decode(retItem)
	return retItem, err
}

func (db *_AppraiseDao) List(ctx context.Context, ideaId string) ([]*AppraiseDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"ideaId": ideaId}, options.Find().SetSort(bson.M{"timeStamp": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*AppraiseDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_AppraiseDao) ListByMember(ctx context.Context, ideaIdList []string, memberUserId string) ([]*AppraiseDbItem, error) {
	idList := []string{}
	for _, ideaId := range ideaIdList {
		idList = append(idList, db.genIdeaAppraiseId(ideaId, memberUserId))
	}
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": idList}})
	if err != nil {
		return nil, err
	}
	retList := []*AppraiseDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_AppraiseDao) RemoveByIdea(ctx context.Context, ideaId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"ideaId": ideaId})
	return err
}

func (db *_AppraiseDao) Remove(ctx context.Context, ideaId, memberUserId string) error {
	ideaAppraiseId := db.genIdeaAppraiseId(ideaId, memberUserId)
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": ideaAppraiseId, "ideaId": ideaId})
	return err
}
